/*
 * @Author: 小豪
 * @Description:
 * @Date: 2023-10-09 18:09:25
 * @LastEditTime: 2024-04-25 17:20:45
 * @FilePath: \bsct\gohub\app\http\controllers\api\v1\auth\signup_controller.go
 */
// Package auth 处理用户身份认证相关逻辑
package auth

import (
	"fmt"
	v1 "gohub/app/http/controllers/api/v1"
	"gohub/app/models/user"
	"gohub/app/requests"
	"gohub/pkg/jwt"
	"gohub/pkg/response"
	"regexp"
	"time"

	"github.com/gin-gonic/gin"
)

// SignupController 注册控制器
type SignupController struct {
	v1.BaseAPIController
}

func validatePhoneNumber(phoneNumber string) bool {
	// 匹配规则
	// ^1第一位为一
	// [345789]{1} 后接一位345789 的数字
	// \\d \d的转义 表示数字 {9} 接9位
	// $ 结束符
	regRuler := "^1[345789]{1}\\d{9}$"

	// 正则调用规则
	reg := regexp.MustCompile(regRuler)

	// 返回 MatchString 是否匹配
	return reg.MatchString(phoneNumber)
}

// IsPhoneExist 检测手机号是否被注册
func (sc *SignupController) IsPhoneExist(c *gin.Context) {

	request := requests.SignupPhoneExistRequest{}
	errs := requests.Validate(c, &request, requests.SignupPhoneExist)
	fmt.Println(errs)
	if len(errs) > 0 {
		// 失败，显示错误提示
		response.JSON(c, gin.H{
			"success":    false,
			"total_page": "",
			"status":     "100",
			"msg":        errs,
			"data":       map[string]string{},
			"time":       time.Now().Unix(),
		})
		return
	}
	exist := user.IsPhoneExist(request.Phone)
	if exist {
		//  检查数据库并返回响应
		response.JSON(c, gin.H{
			"success":    true,
			"total_page": "",
			"status":     "200",
			"msg":        "该账号已注册！",
			"data":       []string{},
			"time":       time.Now().Unix(),
		})
	} else {
		response.JSON(c, gin.H{
			"success":    false,
			"total_page": "",
			"status":     "100",
			"msg":        "该账号不存在！",
			"data":       []string{},
			"time":       time.Now().Unix(),
		})
	}

}

// IsEmailExist 检测邮箱是否已注册
// func (sc *SignupController) IsEmailExist(c *gin.Context) {
// 	request := requests.SignupEmailExistRequest{}
// 	if ok := requests.Validate(c, &request, requests.SignupEmailExist); !ok {
// 		return
// 	}

// 	response.JSON(c, gin.H{
// 		"exist": user.IsEmailExist(request.Email),
// 	})
// }

// SignupUsingPhone 使用手机和验证码进行注册
func (sc *SignupController) SignupUsingPhone(c *gin.Context) {

	// 1. 验证表单
	request := requests.SignupUsingPhoneRequest{}
	errs := requests.Validate(c, &request, requests.SignupUsingPhone)
	if len(errs) > 0 {
		// 失败，显示错误提示
		response.JSON(c, gin.H{
			"success":    false,
			"total_page": "",
			"status":     "100",
			"msg":        errs,
			"data":       map[string]string{},
			"time":       time.Now().Unix(),
		})
		return
	}
	//验证手机号
	exist := user.IsPhoneExist(request.Phone)
	if exist {
		//  检查数据库并返回响应
		response.JSON(c, gin.H{
			"success":    true,
			"total_page": "",
			"status":     "200",
			"msg":        "该账号已注册！",
			"data":       []string{},
			"time":       time.Now().Unix(),
		})
		return
	}
	// 2. 验证成功，创建数据
	_user := user.User{
		Name:     request.Name,
		Phone:    request.Phone,
		Password: request.Password,
	}
	_user.Create()

	if _user.ID > 0 {
		token := jwt.NewJWT().IssueToken(_user.GetStringID(), _user.Name)
		response.JSON(c, gin.H{
			"success":    true,
			"total_page": "",
			"status":     "200",
			"msg":        "注册成功！",
			"data": map[string]string{
				"name":     _user.Name,
				"token":    token,
				"phone":    _user.Phone,
				"password": _user.Password,
			},
			"time": time.Now().Unix(),
		})
		return
	} else {
		response.JSON(c, gin.H{
			"success":    false,
			"total_page": "",
			"status":     "100",
			"msg":        "创建用户失败，请稍后尝试~",
			"data":       []string{},
			"time":       time.Now().Unix(),
		})
		return
	}

}
